package com.shengde.jinwawaforleader.model.homePage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.base.CommonBaseAdapter;
import com.shengde.jinwawaforleader.base.ViewHolder;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProEveryNewsList;
import com.shengde.jinwawaforleader.utils.DateUtil;
import com.umeng.socialize.UMShareAPI;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;


/**
 * 更多热门资讯
 * Created by Administrator on 2016/9/5 0005.
 */
public class HotNewsMoreActivity extends BaseActivity implements AdapterView.OnItemClickListener {
	private static final String TAG = "HotNewsMoreActivity";

	private static final int HOTNEWS_MORE = 1;
	private static final int FIRST_IN = 2;
	private static final int NO_MORE = 3;
	private static final int ADD_SUCCEED = 4;
	private static final int FLUSH_SUCCEED = 5;
	private List<ProEveryNewsList.NewsBean> Newws_bean; //

	private TextView tv_back;
	private TextView tv_title;
	private LinearLayout loading_main;
	private CommonBaseAdapter mHotNewsAdatpter;
	private ListView lv_hotnews_more; //热门咨询的listview
	private PullToRefreshListView pull_refresh_list; //下拉刷新 和上拉加载跟多

	int curpage = 1;
	int totalpages = 1;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
				case FIRST_IN:

					if (mHotNewsAdatpter == null) {
						mHotNewsAdatpter = new CommonBaseAdapter(HotNewsMoreActivity.this, Newws_bean, R.layout.index_school_item) {
							@Override
							public void convert(ViewHolder holder, int position) {
								holder.setText(R.id.tv_school_item_title, Newws_bean.get(position).getTitle())
										.setText(R.id.tv_school_item_time, DateUtil.getDate(String.valueOf(Newws_bean.get(position).getLasttime())));
								LinearLayout convertview = (LinearLayout) holder.getConvertView();

								if (Newws_bean.get(position).getPicturePath()==null||"".equals(Newws_bean.get(position).getPicturePath())){
									holder.setImageResource(R.id.iv_school_item_logo,R.drawable.touxiang);

								}else {
									Glide.with(HotNewsMoreActivity.this)
											.load(BaseProtocol.IMG_BASE + Newws_bean.get(position).getPicturePath())
											.error(R.drawable.touxiang)
											.into((ImageView) convertview.findViewById(R.id.iv_school_item_logo));
								}
							}
						};
						lv_hotnews_more.setAdapter(mHotNewsAdatpter);
						loading_main.setVisibility(View.GONE);
					} else {
						mHotNewsAdatpter.notifyDataSetChanged();
					}

					break;
				case FLUSH_SUCCEED:
					pull_refresh_list.onRefreshComplete();
					mHotNewsAdatpter.notifyDataSetChanged();
					Toast.makeText(HotNewsMoreActivity.this, getString(R.string.refresh_success), Toast.LENGTH_SHORT).show();
					break;
				case ADD_SUCCEED:
					pull_refresh_list.onRefreshComplete();
					mHotNewsAdatpter.notifyDataSetChanged();
					Toast.makeText(HotNewsMoreActivity.this, getString(R.string.loading_success), Toast.LENGTH_SHORT).show();
					break;
				case NO_MORE:
					pull_refresh_list.onRefreshComplete();
					Toast.makeText(HotNewsMoreActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
					break;
			}
		}
	};

	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.hot_news_more_activity);

	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		loading_main = (LinearLayout) findViewById(R.id.loading_main);
		pull_refresh_list = (PullToRefreshListView) findViewById(R.id.lv_hotnews_more);
		lv_hotnews_more = pull_refresh_list.getRefreshableView();

	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.hot_news));
		pull_refresh_list.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载...");
		pull_refresh_list.getLoadingLayoutProxy(false, true).setReleaseLabel("放开以加载...");
		showHotNews(curpage, FIRST_IN);

	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		lv_hotnews_more.setOnItemClickListener(this);
		//刷新监听
		pull_refresh_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
				curpage = 1;
				showHotNews(curpage, FLUSH_SUCCEED);
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
				curpage++;
				if (curpage > totalpages) {
					curpage--;
					mHandler.sendEmptyMessage(NO_MORE);
				} else {
					showHotNews(curpage, ADD_SUCCEED);
				}
			}
		});
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(HotNewsMoreActivity.this,
				InformationItemActivity.class);
		intent.putExtra("noticeId", Newws_bean.get(position - 1)
				.getId() + "");
		intent.putExtra("noticeTitle", Newws_bean.get(position - 1)
				.getTitle());
		intent.putExtra("imgpath", Newws_bean.get(position - 1)
				.getPicturePath());
		startActivity(intent);
	}

	/**
	 * 热门资讯的 网络请求
	 *
	 * @param curpage
	 * @param flag
	 */
	private void showHotNews(int curpage, final int flag) {
		//http://120.76.155.123:8080/api/director/InfosList?type=&curpage=1&teacherid=1
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/director/InfosList";
		} else {
			url = BaseProtocol.HOST_BASE + "/director/InfosList";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("type", 2);
		params.addParameter("curpage", curpage);
		params.addParameter("schoolid", User.getSchoolId(this));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProEveryNewsList pro_hot_news_list = new Gson().fromJson(result, ProEveryNewsList.class);
				if (pro_hot_news_list != null && pro_hot_news_list.getCode() == 0) {
					Newws_bean = pro_hot_news_list.getNews();
					mHandler.sendEmptyMessage(flag);
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
	}
}
